#include "CEntranceServer.h"
#include "CEntranceRunConfig.h"

CEntranceServer::CEntranceServer()
{
	m_runStatus = false;
	context.strAppVersion = T_DevVersion().channelVersion;
	context.strArchInfo = ARCH_INFO;
	context.strDevicesInfo =  CEntranceRunConfig::getSingleton().getDevMode();
	context.strImgVsersion = IMG_VERSION;
	context.strAudioDefualtPath = AUDIO_RES_DEFULAT_PATH;
	context.strAudoUpdatePath = AUDIO_RES_UPDATE_PATH;
	context.bCameraHorizontalPlate = CEntranceRunConfig::getSingleton().cmaerIsHp();
	// 2021-08-30 jxs 修改相机方向为旋转角度
	context.bCameraInitRotationAngle = CEntranceRunConfig::getSingleton().cameraInitRotationAngle();
	context.bIotFunt = CEntranceRunConfig::getSingleton().isIot();
	// 2021-10-12 jxs 修改设备尺寸为string
	context.strDimensions = CEntranceRunConfig::getSingleton().getDimensions();
	// 2021-10-21 jxs 增加局域网室内机呼叫是否增强摄像头效果
	context.LanRoomCallIsEnhanceCamera = CEntranceRunConfig::getSingleton().getLanRoomCallIsEnhanceCamera();
	// 2022-01-22 jxs 增加 升级后清除数据的APP版本
	context.updateCleanDataAppVersion = CEntranceRunConfig::getSingleton().getUpdateCleanDataAppVersion();
}

// 启动门禁服务
bool CEntranceServer::start(int argc, char *argv[])
{
	m_argc = argc;
	m_argv = argv;
	m_runStatus = false;
	// 启动线程
	QThread::start();

	return true;
}
void CEntranceServer::run()
{

	LOGD("---------------- app start run ----------------");
	LOGD("---------------- ARCH_INFO = %s ----------------",context.strArchInfo.c_str());
	LOGD("------------- DEVICES_TYPE = %s ----------------",context.strDevicesInfo.c_str());
	LOGD("-------------- IMG_VERSION = %s ----------------",context.strImgVsersion.c_str());
	LOGD("-------------- APP_VERSION = %s ----------------",context.strAppVersion.c_str());

	// 当系统重启后程序启动很大概率会崩，也不知道那的原因，索性加个延时。
	sleep(2);

	if (CEntranceApplication::Instance().staticInit(&context) == false)
	{
		LOGE("Application staticInit failed!");
		return;
	}

	while(true){
		// 首次启动需要网络，需要等待
		if (CEntranceApplication::Instance().init(m_argc, m_argv))
		{
			break;
		} else {
			LOGE("Application init failed!");
			sleep(5);
		}
	}

	usleep(5000);
	if (CEntranceApplication::Instance().delayedInit() == false)
	{
		LOGE("Application delayedInit failed!");
		return;
	}
	m_runStatus = true;
	// 门禁运行（会阻塞）
	CEntranceApplication::Instance().run();
	// 门禁退出
	CEntranceApplication::Instance().handlerExit();
}


